package work5;

/**
 * @TODO
 * @Author xu
 * 2025/2/26
 */
public class Method {public static void main(String[] args) {
    double sum = calculateSum(20);
    System.out.println("前20项的和是: " + sum);
}

    public static double calculateSum(int n) {
        int numerator1 = 2;
        int numerator2 = 3;
        int denominator1 = 1;
        int denominator2 = 2;
        double sum = 0.0;

        for (int i = 1; i <= n; i++) {
            double fraction = (double) numerator1 / denominator1;
            sum += fraction;
            int tempNumerator = numerator2;
            int tempDenominator = denominator2;
            numerator2 = numerator1 + numerator2;
            denominator2 = denominator1 + denominator2;
            numerator1 = tempNumerator;
            denominator1 = tempDenominator;
        }

        return sum;
    }

}
